/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import Beans.User;
import datas.UserMapper;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.session.SqlSession;

/**

 @author Marie-Claude
 */
public class UserDao {

  public static int getIdByUsername(String username) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {

      UserMapper mapper = session.getMapper(UserMapper.class);
      int id = mapper.getIdByUsername(username);

      return id;
    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static User getByUsername(String username) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {

      UserMapper mapper = session.getMapper(UserMapper.class);
      User user = mapper.getByUsername(username);

      return user;
    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static int deleteAccount(Date lastLogin, int idUser) {

    SqlSession session = MyBatisManager.getSqlSession();

    try {

      UserMapper mapper = session.getMapper(UserMapper.class);
      int id = mapper.fakeDeleteUser(lastLogin, idUser);
      session.commit();

      return id;
    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static User getUserById(int id) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {

      UserMapper mapper = session.getMapper(UserMapper.class);
      User user = mapper.getById(id);

      return user;
    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static void createAccount(User user) {

    SqlSession session = MyBatisManager.getSqlSession();

    try {

      UserMapper mapper = session.getMapper(UserMapper.class);
      mapper.insertFirstUser(user);
      session.commit();

    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static int updateUser(Boolean isWarnedNewEmail, Boolean isWarnedAddWatchList, String password, String ville, int age, String rechercheSexe, String sexe, double taille, String but, String resume, String apparence, int idUser) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {

      UserMapper mapper = session.getMapper(UserMapper.class);
      int retour = mapper.updateUserProfile(isWarnedNewEmail, isWarnedAddWatchList, password, ville, age, rechercheSexe, sexe, taille, but, resume, apparence, idUser);
      session.commit();

      return retour;
    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static List<User> getUserDyn() {
    SqlSession session = MyBatisManager.getSqlSession();

    try {

      UserMapper mapper = session.getMapper(UserMapper.class);
      List<User> lstUsers = mapper.getAllUsers();
      return lstUsers;

    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static List<User> getAll() {
    SqlSession session = MyBatisManager.getSqlSession();

    try {

      UserMapper mapper = session.getMapper(UserMapper.class);
      List<User> lstUsers = mapper.getAllUsers();
      return lstUsers;

    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static int upgradeUserAccount(boolean upgradeRole, int idUser) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {

      UserMapper mapper = session.getMapper(UserMapper.class);
      int id = mapper.upgradeAccount(upgradeRole, idUser);

      session.commit();
      return id;
    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static List<User> selectAllSearch(HashMap monHashMap) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {

      List<User> lstUsers = session.selectList("datas.UserSearchMapper.getAll", monHashMap);

      return lstUsers;
    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static String getEmailsByMail(String email) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {
      UserMapper mapper = session.getMapper(UserMapper.class);
      String emailRetour = mapper.getEmailsByMail(email);

      return emailRetour;
    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static String getUserName(String userName) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {
      UserMapper mapper = session.getMapper(UserMapper.class);
      String nameRetour = mapper.getUserName(userName);

      return nameRetour;
    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static List<String> getPictures(int userID) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {
      UserMapper mapper = session.getMapper(UserMapper.class);
      List<String> pictures = mapper.selectUserPictures(userID);

      return pictures;
    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static void insertPicture(int userID, String path) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {

      UserMapper mapper = session.getMapper(UserMapper.class);
      mapper.insertPicture(userID, path);
      session.commit();

    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static void changeProfilePicture(int userID, String path) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {

      UserMapper mapper = session.getMapper(UserMapper.class);
      mapper.changeProfilePicture(userID, path);
      session.commit();

    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static void updateLastLogin(int userID) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {

      UserMapper mapper = session.getMapper(UserMapper.class);
      mapper.updateLastLogin(userID);
      session.commit();

    } finally {
      MyBatisManager.closeConnexion();
    }
  }
}
